package models

import (
	"os"
	"strings"

	"gorm.io/gorm"
)

type Course struct {
	ID     int `json:"id" gorm:"column:id;"`
	UserID int `json:"user_id" gorm:"column:user_id;"`

	CateId         int            `json:"cate_id" validate:"required,gte=1" gorm:"column:cate_id;"`
	CourseCategory CourseCategory `json:"course_category" gorm:"foreignKey:CateId;references:ID;"`

	TeacherName        string `json:"teacher_name" validate:"required,min=1,max=64" gorm:"column:teacher_name;"`
	TeacherDescription string `json:"teacher_description" validate:"required,min=1,max=255" gorm:"column:teacher_description;"`
	TeacherOrg         string `json:"teacher_org" validate:"max=64" gorm:"column:teacher_org;"`
	TeacherGender      int    `json:"teacher_gender" validate:"gte=0,lte=1" gorm:"column:teacher_gender;"`
	TeacherContact     string `json:"teacher_contact" validate:"required,max=64" gorm:"column:teacher_contact;"`

	Name         string            `json:"name" validate:"required,min=1,max=64" gorm:"column:name;"`
	Introduction string            `json:"introduction" validate:"required,min=1,max=255" gorm:"column:introduction;"`
	Cover        string            `json:"cover" validate:"-" gorm:"column:cover;"`
	Address      string            `json:"address" validate:"required,min=1,max=1024" gorm:"column:address;"`
	CountyIndex  int               `json:"county_index" validate:"-" gorm:"column:county_index;"`
	CourseHours  int               `json:"course_hours" validate:"required,gte=1" gorm:"column:course_hours;"`
	CourseTime   string            `json:"course_time" validate:"required,min=1,max=4096" gorm:"column:course_time;"`
	IsNonProfit  int               `json:"is_non_profit" validate:"gte=0,lte=1" gorm:"column:is_non_profit;"`
	CapacityMin  int               `json:"capacity_min" validate:"required,gte=1" gorm:"column:capacity_min;"`
	CapacityMax  int               `json:"capacity_max" validate:"required,gte=1,gtefield=CapacityMin" gorm:"column:capacity_max;"`
	EnrollEndAt  JSONTimeNoSeconds `json:"enroll_end_at" validate:"required,jsontime_no_seconds" gorm:"column:enroll_end_at;type:datetime"`
	StartAt      JSONTimeNoSeconds `json:"start_at" validate:"-" gorm:"column:start_at;type:datetime"`
	EndAt        JSONTimeNoSeconds `json:"end_at" validate:"-" gorm:"column:end_at;type:datetime"`

	EnrollAmount         int `json:"enroll_amount" gorm:"column:enroll_amount;<-:update"`
	EnrollApprovedAmount int `json:"enroll_approved_amount" gorm:"column:enroll_approved_amount;<-:update"`

	Status AuditStatus `json:"status" gorm:"column:status;<-:update"`

	IsSubscribed int `json:"-" gorm:"column:is_subscribed;" validate:"-"`
	VisitAmount  int `json:"visit_amount" gorm:"column:visit_amount;<-"`

	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;"`
}

type CourseRecord struct {
	Course
	MyEnrollment Enrollment `json:"my_enrollment" gorm:"-" validate:"-"`
}

type EnrollmentCount struct {
	Count          int `json:"count" gorm:"-" validate:"-"`
	UnhandledCount int `json:"unhandled_count" gorm:"-" validate:"-"`
}

type CommentCount struct {
	Count          int `json:"count" gorm:"-" validate:"-"`
	UnhandledCount int `json:"unhandled_count" gorm:"-" validate:"-"`
}

type CourseWithEnrollmentAndComment struct {
	Course
	Enrollment EnrollmentCount `json:"enrollment" gorm:"-" validate:"-"`
	Comment    CommentCount    `json:"comment" gorm:"-" validate:"-"`
}

func (c *Course) AfterFind(tx *gorm.DB) (err error) {
	if c.Cover != "" {
		c.Cover = os.Getenv("STATIC_RESOURCE_URL") + c.Cover
	} else {
		c.Cover = os.Getenv("STATIC_RESOURCE_URL") + "images/default_cover.jpg?v=1"
	}
	return
}

func (c *Course) BeforeUpdate(tx *gorm.DB) (err error) {
	if strings.HasPrefix(c.Cover, os.Getenv("STATIC_RESOURCE_URL")) {
		c.Cover = c.Cover[len(os.Getenv("STATIC_RESOURCE_URL")):]
	}
	return
}
